/**
 * 
 */
package com.kebo2.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;


/**
 * @author Tim
 *This class is used to search content in the database. The content is shown via 
 *the ShowAndParseResult class.
 */
public class Search {
	
	private int cb;
	private VerticalPanel vp;
	private DisclosurePanel advancedSearchPanel;
	private FlexTable ft;
	private SuggestBox searchQuery;
	private Label infoLb;
	private CheckBox advncd1;
	private CheckBox advncd2;
	private CheckBox advncd3;
	private CheckBox advncd4;

	public Search(boolean currentMode, int cb) {
		this.cb = cb;
		if(currentMode == true)
			bookMode();
		else
			userMode();	
	}
	
	public static void DirectSearch(String query) {
		searchBookInDatabase(query, false, false, false, false);
	}
	
	/**
	 * @wbp.parser.entryPoint
	 */
	private void bookMode() {
		vp = new VerticalPanel();
		
		searchQuery = new SuggestBox();
		searchQuery.addKeyDownHandler(new KeyDownHandler() {
			public void onKeyDown(KeyDownEvent event) {
				if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					if(searchQuery.getValue().length() > 1) {
						searchBookInDatabase(searchQuery.getValue(), advncd1.getValue(), advncd2.getValue(), advncd3.getValue(), advncd4.getValue());
					}
					else {
						new UserInterface.notification("Vul een zoekwoord in.");
					}	
				}
			}
		});
		/*searchQuery.addValueChangeHandler(new ValueChangeHandler<String>() {
			public void onValueChange(ValueChangeEvent<String> event) {
				liveSearch(event.getValue());
			}
		});*/
		vp.add(searchQuery);
		searchQuery.setWidth("272px");
		
		advancedSearchPanel = new DisclosurePanel("Geavanceerd zoeken");
		advancedSearchPanel.setAnimationEnabled(true);
		vp.add(advancedSearchPanel);
		advancedSearchPanel.setSize("272px", "110px");
		
		ft = new FlexTable();
		advancedSearchPanel.setContent(ft);
		ft.setSize("272px", "4cm");
		
		infoLb = new Label("Enkel zoeken in:");
		ft.setWidget(0, 0, infoLb);
		
		advncd1 = new CheckBox("Titels");
		ft.setWidget(1, 0, advncd1);
		
		advncd2 = new CheckBox("Auteurs");
		ft.setWidget(2, 0, advncd2);
		
		advncd3 = new CheckBox("Genre");
		ft.setWidget(3, 0, advncd3);
		
		advncd4 = new CheckBox("Plaats");
		ft.setWidget(4, 0, advncd4);
		
		switch(cb) {
		case 0:
			new UserInterface.Window(vp, "Zoek media", 280, 220);
		break;
		case 1:
			new UserInterface.Window(vp, "Zoek media om te bewerken", 280, 220);
		break;
		case 2:
			new UserInterface.Window(vp, "Zoek media om te verwijderen", 280, 220);
		break;	
		case 3:
			new UserInterface.Window(vp, "Zoek media om uit te lenen", 280, 220);
		break;
		}
		
		new UserInterface.Icon("search", 4);
		ClickHandler handler = new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(searchQuery.getValue().length() > 1) {
					searchBookInDatabase(searchQuery.getValue(), advncd1.getValue(), advncd2.getValue(), advncd3.getValue(), advncd4.getValue());
				}
				else {
					new UserInterface.notification("Vul een zoekwoord in.");
				}				
			}	
		};
		UserInterface.Icon.searchImg.addClickHandler(handler);
		UserInterface.Icon.searchTxt.addClickHandler(handler);
	}	

	private void userMode() {
		vp = new VerticalPanel();
		
		searchQuery = new SuggestBox();
		searchQuery.addValueChangeHandler(new ValueChangeHandler<String>() {
			public void onValueChange(ValueChangeEvent<String> event) {
				liveSearch(event.getValue());
			}
		});
		vp.add(searchQuery);
		searchQuery.setWidth("272px");
		
		infoLb = new Label("Kebo zoekt automatisch op gebruikersnaam en groep.");
		vp.add(infoLb);
		
		switch(cb) {
		case 0:
			new UserInterface.Window(vp, "Zoek gebruikers", 280, 100);
		break;
		
		case 1:
			new UserInterface.Window(vp, "Zoek gebruikers om te bewerken", 280, 100);
		break;
		
		case 2:
			new UserInterface.Window(vp, "Zoek gebruikers om te verwijderen", 280, 100);
		break;	
		
		case 3:
			new UserInterface.Window(vp, "Zoek gebruikers om media aan uit te lenen", 280, 100);
		break;
		
		case 4:
			new UserInterface.Window(vp, "Zoek gebruikers om media binnen te brengen", 280, 100);
		break;
		}
		
		new UserInterface.Icon("search", 4);
		ClickHandler handler = new ClickHandler() {
			public void onClick(ClickEvent event) {
				searchUserInDatabase(searchQuery.getValue());
			}	
		};
		UserInterface.Icon.searchImg.addClickHandler(handler);
		UserInterface.Icon.searchTxt.addClickHandler(handler);
		
	}

	private static void searchBookInDatabase(String query, Boolean advncd1, Boolean advncd2, Boolean advncd3, Boolean advncd4) {
		String bounds = "0";
		if(advncd1 == true)
			bounds = bounds + "title";
		if(advncd2 == true)
			bounds = bounds + "author";
		if(advncd3 == true)
			bounds = bounds + "genre";
		if(advncd4 == true)
			bounds = bounds + "place";
		
		String file = "searchBook.php?query=" + query + "&bounds=" + bounds;
		HttpRequest object = new HttpRequest();
		object.getMessageXml(file, "GET", null, new AsyncCallback<String>() {
			public void onSuccess(String result) {
				new ShowAndParseResult(true, result);
			}
			public void onFailure(Throwable caught) {
				new UserInterface.notification(Kebo2.error.INTERNET_CONNECTION);		
			}
		});
		
		UserInterface.clearIcons();
		UserInterface.closeWindow();
	}
	
	protected void liveSearch(String query) {
		// TODO Auto-generated method stub
		
	}
	
	private static void searchUserInDatabase(String query) {
		String file = "searchUser.php?query=" + query;
		HttpRequest object = new HttpRequest();
		object.getMessageXml(file, "GET", null, new AsyncCallback<String>() {
			public void onSuccess(String result) {
				new ShowAndParseResult(false, result);
			}
			public void onFailure(Throwable caught) {
				new UserInterface.notification(Kebo2.error.INTERNET_CONNECTION);		
			}
		});
		
		UserInterface.clearIcons();
		UserInterface.closeWindow();
	}
}
